﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace auto_win
{
    internal class API
    {
        //private static string domain = "http://localhost:8181/auto";
        private static string domain = "https://redbees.cn/auto";

        private static string token = null;

        public static User signin(string phone, string passwd)
        {
            string url = domain + $"/auth/app-signin?phone={phone}&passwd={passwd}";
            Trace.WriteLine(url);
            WebRequest request = WebRequest.Create(url);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(dataStream);
                    string data = reader.ReadToEnd();
                    Result<User> r = JsonConvert.DeserializeObject<Result<User>>(data);
                    if (r.success)
                    {     
                        token = r.data.token;
                        return r.data;
                    }
                    MessageBox.Show(r.msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return null;
                }
            }
        }

        public static User getInfo()
        {
            string url = domain + $"/user/getInfo2";
            Trace.WriteLine(url);
            WebRequest request = WebRequest.Create(url);
            request.Headers.Add("t", token);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(dataStream);
                    string data = reader.ReadToEnd();
                    Result<User> r = JsonConvert.DeserializeObject<Result<User>>(data);
                    if (r.success)
                    {
                        return r.data;
                    }
                    MessageBox.Show(r.msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return null;
                }
            }
        }

        public static List<WechatCrowd> getCrowd()
        {
            string url = domain + $"/crowd/my";
            Trace.WriteLine(url);
            WebRequest request = WebRequest.Create(url);
            request.Headers.Add("t", token);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(dataStream);
                    string data = reader.ReadToEnd();
                    Result<List<WechatCrowd>> r = JsonConvert.DeserializeObject<Result<List<WechatCrowd>>>(data);
                    if (r.success)
                    {
                        return r.data;
                    }
                    MessageBox.Show(r.msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return null;
                }
            }
        }

        public static List<CrowdTask> getTasks()
        {
            string url = domain + $"/config/all";
            WebRequest request = WebRequest.Create(url);
            request.Headers.Add("t", token);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(dataStream);
                    string data = reader.ReadToEnd();
                    Trace.WriteLine(data);
                    Result<List<CrowdTask>> r = JsonConvert.DeserializeObject<Result<List<CrowdTask>>>(data);
                    if (r.success)
                    {
                        return r.data;
                    }
                    MessageBox.Show(r.msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return null;
                }
            }
        }

        public static List<Product> getProducts()
        {
            string url = domain + $"/order/products";
            WebRequest request = WebRequest.Create(url);
            request.Headers.Add("t", token);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(dataStream);
                    string data = reader.ReadToEnd();
                    Trace.WriteLine(data);
                    Result<List<Product>> r = JsonConvert.DeserializeObject<Result<List<Product>>>(data);
                    if (r.success)
                    {
                        return r.data;
                    }
                    MessageBox.Show(r.msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return null;
                }
            }
        }

        public static string getQRCode(int months)
        {
            string url = domain + $"/pay/pre-order?months={months}";
            WebRequest request = WebRequest.Create(url);
            request.Headers.Add("t", token);
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(dataStream);
                    string data = reader.ReadToEnd();
                    Trace.WriteLine(data);
                    Result<string> r = JsonConvert.DeserializeObject<Result<string>>(data);
                    if (r.success)
                    {
                        return r.data;
                    }
                    MessageBox.Show(r.msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return null;
                }
            }
        }


        public static void saveFriends(List<CrowdMember> friends)
        { 
            string url = domain + "/crowd/saveFriend";
            post(url, friends); ;
        }
        public static void saveJoins(List<CrowdMember> joins)
        {
            string url = domain + "/crowd/saveJoin";
            post(url, joins); ;
        }


        public static void sendMessage(Message msg)
        {
            string url = domain + "/message/sendMessage";
            post(url, msg);
        }

        public static void post(string url, object msg)
        {
            WebRequest request = WebRequest.Create(url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.Headers.Add("t", token);
            string jsonData = JsonConvert.SerializeObject(msg);
            byte[] byteArray = Encoding.UTF8.GetBytes(jsonData);
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream dataStream = response.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(dataStream);
                    string data = reader.ReadToEnd();
                    Result<string> r = JsonConvert.DeserializeObject<Result<string>>(data);
                    if (!r.success)
                    {
                        MessageBox.Show(r.msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }
    }


    class Result<T>
    {
        public int code;
        public String msg;
        public bool success;
        public T data;
    }

    public class User
    {
        public string phone;
        public DateTime endAt;
        public string token;
    }

    public class Product
    {
        public int months;

        public String name;

        public int price; //单位：分
    }

    public class WechatCrowd
    {
        public int id;
        public string crowdName;
        public string wechatNickname;
        public string comment;
    }

    public class CrowdTask
    {
        public int id;
        public int type;
        public string message;
        public string startTime;
        public string endTime;
        public int interval;
    }

    public class CrowdMember
    {
        public int crowdId;
        public string wechatId;
        public string crowdname;
        public string nickname;
        public string comment; //备注名字
        public string area;
        public string inviteBy; //推荐人的微信昵称
        public override bool Equals(object obj)
        {
            // 如果对象引用相同，则直接返回true
            if (obj == this) return true;

            // 如果obj不是Person类型，则返回false
            if (obj == null || GetType() != obj.GetType()) return false;
            CrowdMember other = (CrowdMember)obj;
            return crowdname == other.crowdname && nickname == other.nickname;
        }
        public override int GetHashCode()
        {
            return (crowdname != null ? crowdname.GetHashCode() : 0) ^ (nickname != null ? nickname.GetHashCode() : 0);
        }
    }

    public class Message
    {
        public Message(string crowdName, int type, string wechatNickname, string content)
        {
            this.crowdName = crowdName;
            this.type = type;
            this.wechatNickname = wechatNickname;
            this.content = content;
        }
        public string crowdName;
        public int type; //0 群发， 1 进群， 2 退群， 3 加好友
        public string wechatNickname;
        public string content;
    }
}
